package com.holly.unit.deform.modular.controller;

import com.alibaba.fastjson.JSONObject;
import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.core.pojo.response.SuccessResponseData;
import com.holly.unit.deform.api.DeformEngineApi;
import com.holly.unit.deform.util.QiniuUtil;
import com.holly.unit.scanner.api.annotation.ApiResource;
import com.holly.unit.scanner.api.annotation.GetResource;
import com.holly.unit.scanner.api.annotation.PostResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 类描述: 表单设计引擎插件
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/12 11:28
 */
@RestController
@ApiResource(name = "表单设计引擎插件")
@Api(tags = "表单设计引擎插件")
public class DeformEngineController {

    @Resource
    private DeformEngineApi api;

    @ApiOperation(value="获取token", notes="获取token")
    @GetResource(name = "获取token" , path = "/designFormApi/getQiniuUploadToken")
    public ResponseData getQiniuUploadToken() {
        return new SuccessResponseData(QiniuUtil.getUploadToken());
    }

    /**
     * 自定义表单设计器
     * 检验 Token
     */
    public ModelAndView doIndex(ModelAndView modelAndView, HttpServletRequest request){
        return (ModelAndView)api.doIndex(modelAndView,request);
    }

    @ApiOperation(value="获取desgForm", notes="获取desgForm")
    @GetResource(name = "获取desgForm" , path = "/designFormApi/index/{id}")
    public ModelAndView desgForm(@PathVariable(value = "id",required = false) String id, @RequestParam(value = "isTemplet" ,required = false) Boolean isTemplet, ModelAndView modelAndView, HttpServletRequest request) {
        return (ModelAndView) api.desgForm(id,isTemplet,modelAndView,request);
    }

    /**
     * 表单编辑、修改
     */
    @ApiOperation(value="表单编辑、修改", notes="表单编辑、修改")
    @PostResource(name = "表单编辑、修改" , path = "/designFormApi/view/{desformId}/{dataId}")
    public ModelAndView formView(@PathVariable("desformId") String desformId, @PathVariable("dataId") String dataId, ModelAndView modelAndView, HttpServletRequest request) {
        return (ModelAndView)api.formView(desformId,dataId,modelAndView,request);
    }

    /**
     * 表单新增
     */
    @ApiOperation(value="表单新增", notes="表单新增")
    @GetResource(name = "表单新增" , path = "/designFormApi/add/{desformCode}")
    public ModelAndView addDesformData(@PathVariable("desformCode") String desformCode, ModelAndView modelAndView, HttpServletRequest request) {
        return (ModelAndView)api.addDesformData(desformCode,modelAndView,request);
    }

    /**
     * 表单编辑修改
     */
    @ApiOperation(value="表单编辑修改", notes="表单编辑修改")
    @GetResource(name = "表单编辑修改" , path = "/designFormApi/edit/{desformCode}/{dataId}")
    public ModelAndView formEditByCode(@PathVariable("desformCode") String desformCode, @PathVariable("dataId") String dataId, ModelAndView modelAndView, HttpServletRequest request) {
        return (ModelAndView)api.formEditByCode(desformCode,dataId,modelAndView,request);
    }


    /**
     * 表单编辑修改
     */
    @ApiOperation(value="直接通过online dataid进行渲染", notes="直接通过online dataid进行渲染")
    @PostResource(name = "直接通过online dataid进行渲染" , path = "/designFormApi/edit/online/{desformCode}/{onlineDataId}")
    public ModelAndView formEditByCodeByOnline(@PathVariable("desformCode") String desformCode, @PathVariable("onlineDataId") String onlineDataId, ModelAndView modelAndView, HttpServletRequest request) {
        return (ModelAndView)api.formEditByCodeByOnline(desformCode,onlineDataId,modelAndView,request);
    }


    /**
     * 表单预览
     *  示例： desform/detail/cc_user_sjq/1252475006019010561
     */
    @ApiOperation(value="表单预览", notes="表单预览")
    @GetResource(name = "表单预览" , path = "/designFormApi/detail/{desformCode}/{dataId}")
    public ModelAndView formDetail(@PathVariable("desformCode") String desformCode, @PathVariable("dataId") String dataId, ModelAndView modelAndView, HttpServletRequest request) {
        return (ModelAndView)api.formDetail(desformCode,dataId,modelAndView,request);
    }

    /**
     * 表单预览
     */
    @ApiOperation(value="表单预览", notes="表单预览")
    @PostResource(name = "表单预览" , path = "/designFormApi/detail/online/{desformCode}/{onlineDataId}")
    public ModelAndView formDetailByOnline(@PathVariable("desformCode") String desformCode, @PathVariable("onlineDataId") String onlineDataId, ModelAndView modelAndView, HttpServletRequest request) {
        return (ModelAndView)api.formDetailByOnline(desformCode,onlineDataId,modelAndView,request);
    }


    /**
     * 自定义接收URL：将数据抽取到新的表单
     * 【测试示例】
     * @param json
     * @param request
     * @return
     */
    @ApiOperation(value="测试示例", notes="测试示例")
    @PostResource(name = "测试示例" , path = "/designFormApi/customUrlTest")
    public ResponseData customUrlTest(@RequestBody JSONObject json, HttpServletRequest request) {
        return api.customUrlTest(json,request);
    }
}
